package com.z996.spring6;

import com.z996.spring6.pojo.HelloWorld;
import com.z996.spring6.server.UserInfoService;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 功能简述:<br>
 * 实验一：获取bean
 *
 * @Author zfh18
 * @Date 2024-02-02 15:27
 * @Version 1.0
 **/
public class GetBeanTest {
    private Logger logger = LoggerFactory.getLogger(GetBeanTest.class);

    /**
     * @Description 根据id获取
     * @Author zfh18
     * @Date 15:38 2024-02-02
     **/
    @Test
    public void testByID() {
        logger.info("测试方法 根据id获取 [开始]");
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-GetBean.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
        helloWorld.sayHello();
        logger.info("测试方法 根据id获取 [结束]");

    }


    /**
     * @Description 根据类型获取
     * @Author zfh18
     * @Date 15:38 2024-02-02
     **/
    @Test
    public void testByClass() {
        logger.info("测试方法 根据类型获取 [开始]");
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-GetBean.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean(HelloWorld.class);
        helloWorld.sayHello();
        logger.info("测试方法 根据类型获取 [结束]");
    }

    /**
     * @Description 根据接口类型获取
     * @Author zfh18
     * @Date 15:38 2024-02-02
     **/
    @Test
    public void testByInterface() {
        logger.info("测试方法 根据接口类型获取 [开始]");
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-GetBean.xml");
        UserInfoService uis = (UserInfoService) context.getBean(UserInfoService.class);
        logger.info("对象地址信息:{}", uis);
        logger.info("测试方法 根据接口类型获取 [结束]");
    }

    /**
     * @Description 根据id 和 类型获取
     * @Author zfh18
     * @Date 15:38 2024-02-02
     **/
    @Test
    public void testByClassAndId() {
        logger.info("测试方法 根据类型获取 [开始]");
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-GetBean.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld", HelloWorld.class);
        helloWorld.sayHello();
        logger.info("测试方法 根据类型获取 [结束]");
    }


}
